package blog.db;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import blog.util.StringUtils;

public class BlogAccount implements IStorableRecordWithIndex {
	public static final int TYPE_MOVABLE = 1;
	public static final int TYPE_METAWEBLOG = 2;
	public static final int TYPE_BLOGGER = 3;
	public static final int TYPE_WORDPRESS = 4;

	public static final int FIELD_ACCOUNT_NAME = 1;
	public static final int FIELD_USER_NAME = 2;
	public static final int FIELD_PASSWORD = 3;
	public static final int FIELD_FULL_URL = 4;
	public static final int FIELD_BLOG_TYPE = 5;
	public static final int FIELD_BLOG_ID = 6;
	public static final int FIELD_BLOG_URL = 7;
	public static final int FIELD_CHECKED = 8;
	
	private String accountName;
	private String userName;
	private String password;
	private String fullUrl;
	private int blogType;
	private String blogId;
	private String blogUrl;
	private boolean checked;
	private int lenEntryList;
	private int lenHistoryList;
	
	private int m_id;
	private int m_indexId;
	
//	public BlogAccount(String accountName, String userName, String password, String fullUrl, int blogType, String blogId, String blogUrl) {
//		this.accountName = accountName;
//		this.userName = userName;
//		this.password = password;
//		this.fullUrl = fullUrl;
//		this.blogType = blogType;
//		this.blogId = blogId;
//		this.blogUrl = blogUrl;
//	}

	public BlogAccount()
	{
		this.accountName = "";
		this.userName = "";
		password = "";
		fullUrl = "http://";
		blogType = TYPE_MOVABLE;
		blogId = "";
		blogUrl = "http://";
		checked = false; 
		
		lenEntryList = 10;
		lenHistoryList = 30;
	}

	public int compare(int field, IStorableRecordWithIndex another) {
		BlogAccount tmpObj = (BlogAccount) another;
		switch( field )
		{
		case FIELD_ACCOUNT_NAME:
			return this.accountName.compareTo( tmpObj.accountName );
		case FIELD_USER_NAME:
			return this.userName.compareTo( tmpObj.userName );
		case FIELD_BLOG_TYPE:
			return this.blogType - tmpObj.blogType;
		}
		return 0;
	}

	public int compareField(int field, Object fieldValue) {
		switch( field )
		{
		case FIELD_ACCOUNT_NAME:
			return this.accountName.compareTo((String)fieldValue);
		case FIELD_USER_NAME:
			return this.userName.compareTo( (String) fieldValue);
		case FIELD_BLOG_TYPE:
			return this.blogType - ((Integer)fieldValue).intValue();
		}
		return 0;
	}

	public void deserializeIndex(DataInputStream dis) throws IOException {
		this.accountName = dis.readUTF();
		this.checked = dis.readBoolean( );
	}

	public int getIndexId() {
		return this.m_indexId;
	}

	public boolean isIndexField(int field) {
		return (field==FIELD_ACCOUNT_NAME) || (field==FIELD_CHECKED);
	}

	public void serializeIndex(DataOutputStream dos) throws IOException {
		dos.writeUTF( StringUtils.null2empty(this.accountName) );
		dos.writeBoolean( this.checked );
	}

	public void setIndexId(int indexId) {
		this.m_indexId = indexId;
	}

	public void deserialize(DataInputStream dis) throws IOException {
		this.deserializeIndex(dis);
		this.blogId = dis.readUTF();
		this.blogType = dis.readInt();
		this.fullUrl = dis.readUTF();
		this.password = dis.readUTF();
		this.userName = dis.readUTF();
		this.blogUrl = dis.readUTF();
		this.lenEntryList = dis.readInt();
		this.lenHistoryList = dis.readInt();
	}

	public int getId() {
		return this.m_id;
	}

	public void serialize(DataOutputStream dos) throws IOException {
		this.serializeIndex(dos);
		dos.writeUTF( StringUtils.null2empty(this.blogId) );
		dos.writeInt( this.blogType );
		dos.writeUTF( StringUtils.null2empty(this.fullUrl) );
		dos.writeUTF( StringUtils.null2empty(this.password) );
		dos.writeUTF( StringUtils.null2empty(this.userName) );
		dos.writeUTF( StringUtils.null2empty(this.blogUrl) );
		dos.writeInt( this.lenEntryList );
		dos.writeInt( this.lenHistoryList );
	}

	public void setId(int id) {
		this.m_id = id;
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public String getBlogId() {
		return blogId;
	}

	public void setBlogId(String blogId) {
		this.blogId = blogId;
	}

	public int getBlogType() {
		return blogType;
	}

	public void setBlogType(int blogType) {
		this.blogType = blogType;
	}

	public String getFullUrl() {
		return fullUrl;
	}

	public void setFullUrl(String fullUrl) {
		this.fullUrl = fullUrl;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getBlogUrl() {
		return blogUrl;
	}

	public void setBlogUrl(String blogUrl) {
		this.blogUrl = blogUrl;
	}
	
	public boolean isMovableType()
	{
		return this.blogType==TYPE_MOVABLE;
	}
	
	public boolean isMetaWeblog()
	{
		return this.blogType==TYPE_METAWEBLOG || blogType==TYPE_WORDPRESS;
	}
	
	public boolean isBlogger()
	{
		return blogType==TYPE_BLOGGER;
	}

	public int getLenEntryList() {
		return lenEntryList;
	}

	public void setLenEntryList(int lenEntryList) {
		this.lenEntryList = lenEntryList;
	}

	public int getLenHistoryList() {
		return lenHistoryList;
	}

	public void setLenHistoryList(int lenHistoryList) {
		this.lenHistoryList = lenHistoryList;
	}

	public boolean isChecked()
	{
		return checked;
	}

	public void setChecked(boolean checked)
	{
		this.checked = checked;
	}
}